

package com.seven.tools.mybatilsx.bean;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;

public class ApiResult extends HashMap<String, Object> {
    public static final String CODE_TAG = "code";
    public static final String MSG_TAG = "msg";
    public static final String DATA_TAG = "data";
    public static final String PAGE_NO_TAG = "page_no";
    public static final String PAGE_SIZE_TAG = "page_size";
    public static final String TOTAL_TAG = "total";
    public static final String RECORDS_TAG = "records";
    public static final String EXTRA_TAG = "extra_information";

    public <T> T parseOtherApiResult(T t) {
        return (T) this;
    }

    public static ApiResult success() {
        return success("操作成功");
    }

    public static ApiResult successMsg(String msg) {
        return success(msg, (Object) null);
    }

    public static ApiResult success(String msg, Object data) {
        return new ApiResult(ApiResultType.SUCCESS, msg, data);
    }

    public boolean isSuccess() {
        return ApiResultType.SUCCESS.value() == this.getCode();
    }

    public static ApiResult success(long pageNo, long pageSize, long total, List records) {
        if (records == null) {
            records = new ArrayList(0);
        }

        return new ApiResult(ApiResultType.SUCCESS, "操作成功", pageNo, pageSize, total, (List) records);
    }

    public static ApiResult success(long pageNo, long pageSize, long total, List records, Object extra) {
        if (records == null) {
            records = new ArrayList(0);
        }

        return new ApiResult(ApiResultType.SUCCESS, "操作成功", pageNo, pageSize, total, (List) records, extra);
    }

    public static <T> ApiResult successList(List<T> records) {
        if (records == null) {
            records = new ArrayList(0);
        }

        return new ApiResult(ApiResultType.SUCCESS, "操作成功", (List) records);
    }


    public static ApiResult success(Object data) {
        return data instanceof String ? successMsg((String) data) : success("操作成功", data);
    }

    public static ApiResult error() {
        return error("操作失败");
    }

    public static ApiResult errorWithCode(int code, String msg) {
        return new ApiResult(code, msg);
    }

    public static ApiResult error(String msg) {
        return error(msg, (Object) null);
    }

    public static ApiResult error(Object data) {
        return error("操作失败", data);
    }

    public static ApiResult error(String msg, Object data) {
        return new ApiResult(ApiResultType.ERROR, msg, data);
    }

    public static ApiResult error(long pageNo, long pageSize, long total, List records) {
        if (records == null) {
            records = Collections.emptyList();
        }

        return new ApiResult(ApiResultType.ERROR, "操作失败", pageNo, pageSize, total, records);
    }

    public static ApiResult error(List<Object> records) {
        if (records == null) {
            records = Collections.emptyList();
        }

        return new ApiResult(ApiResultType.ERROR, "操作失败", records);
    }

    public static ApiResult unAuthorized() {
        return new ApiResult(ApiResultType.UNAUTHORIZED, "无访问权限", (List) null);
    }

    public static ApiResult unAuthenticated() {
        return new ApiResult(ApiResultType.NOT_ACCEPTABLE, "未认证，请登录！", (List) null);
    }

    public ApiResult() {
    }

    public ApiResult(int code, String msg) {
        super.put("code", code);
        super.put("msg", msg);
    }

    public ApiResult(ApiResultType type, String msg, Object data) {
        super.put("code", type.value());
        super.put("msg", msg);
        if (data != null) {
            super.put("data", data);
        }

    }

    public ApiResult(ApiResultType type, String msg, long pageNo, long pageSize, long total, List records) {
        HashMap<String, Object> pageMap = new HashMap();
        pageMap.put("page_no", pageNo);
        pageMap.put("page_size", pageSize);
        pageMap.put("total", total);
        pageMap.put("records", records);
        super.put("code", type.value());
        super.put("msg", msg);
        super.put("data", pageMap);
    }

    public ApiResult(ApiResultType type, String msg, long pageNo, long pageSize, long total, List records, Object extra) {
        HashMap<String, Object> pageMap = new HashMap();
        pageMap.put("page_no", pageNo);
        pageMap.put("page_size", pageSize);
        pageMap.put("total", total);
        pageMap.put("records", records);
        pageMap.put("extra_information", extra);
        super.put("code", type.value());
        super.put("msg", msg);
        super.put("data", pageMap);
    }

    public ApiResult(ApiResultType type, String msg, List records) {
        HashMap<String, Object> pageMap = new HashMap();
        pageMap.put("records", records);
        super.put("code", type.value());
        super.put("msg", msg);
        super.put("data", pageMap);
    }

    public int getCode() {
        return (Integer) this.getOrDefault("code", 500);
    }

    public String getMsg() {
        return this.getOrDefault("msg", "").toString();
    }

    public Object getData() {
        return this.getOrDefault("data", "");
    }
}
